package com.cn.ccp.service.course;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.RowMapper;

import com.cn.ccp.bean.FilePathBean;
import com.cn.ccp.service.BasicService;

public class FilePathService extends BasicService{
	private Logger log = Logger.getLogger(FilePathService.class);
	public FilePathBean getFlagBean(){
		String sql = "SELECT * FROM file_path WHERE flag = true";
		try{
			return (FilePathBean) jdbcTemplate.queryForObject(sql, new FilePathRowMapper());
		}catch(Exception e){
			log.error("query file path setting failed : ", e);
			return null;
		}
	}
	
	
	public FilePathBean findFilePathById(int id) {
		try {
			String sql = "SELECT * FROM file_path WHERE id = ?";
			return (FilePathBean) jdbcTemplate.queryForObject(sql, new Object[] { id }, new FilePathRowMapper());
		} catch (Exception e) {
			// TODO: handle exception
		}
		return new FilePathBean();
	}
	
	
	 public boolean saveFilePathBean(FilePathBean bo){
		 String sql = "insert into file_path(path,flag,backup,createtime,updatetime) values (?,? ,?,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)";
		 return jdbcTemplate.update(sql ,new Object[]{bo.getPath(),bo.isFlag() ,bo.getBackup()}) > 0;
	 } 
	
	 public boolean updateFilePath(FilePathBean bo){
			String sql = "update file_path set path = ?, flag = ? ,backup=? ,updatetime = CURRENT_TIMESTAMP where id = ?";
			return jdbcTemplate.update(sql ,new Object[]{bo.getPath(),bo.isFlag(),bo.getBackup() ,bo.getId()}) > 0; 
	}
	 
	 public boolean deleteFilePath(int id) {
			String sql = "delete from file_path where id = ?";
			try {
				return jdbcTemplate.update(sql ,new Object[]{id}) > 0;
			} catch (Exception e) {
				return false;
			}
		}
	 
	 public List<FilePathBean> getAllPaths(){
			String sql = "SELECT *  FROM file_path ";
			return jdbcTemplate.query(sql,new FilePathRowMapper());
		}
	 
	 public void updateFlag(){
		 String sql = "update file_path set flag = false ";
		 jdbcTemplate.update(sql);
	 }
	 
	public boolean pathIsUsed(int id,String path){
		String sql = "";
		Object[] objs ;
		if(id == 0 ){
			sql = "SELECT * FROM file_path WHERE path = ? ";
			objs = new Object[] { path };
		}else{
			sql = "SELECT * FROM file_path WHERE path = ? and id != ?";
			objs = new Object[] { path, id };
		}
		return jdbcTemplate.query(sql, objs, new FilePathRowMapper()).size() > 0;
	}
	 
	 
	class FilePathRowMapper implements RowMapper {
		public Object mapRow(ResultSet rs, int i) throws SQLException {
			FilePathBean bean = new FilePathBean();
			bean.setId(rs.getInt("id"));
			bean.setPath(rs.getString("path"));
			bean.setFlag(rs.getBoolean("flag"));
			if(bean.isFlag())
				bean.setPathStatus("正常");
			else
				bean.setPathStatus("禁用");
			
			bean.setBackup(rs.getString("backup"));
			bean.setCreateTime(rs.getString("createtime").substring(0, 19));
			bean.setUpdateTime(rs.getString("updatetime").substring(0, 19));
			
			return bean;
		}
	}
}
